<?php
defined('IN_IA') or exit('Access Denied');
global $_W,$_GPC;
define('DM_PATH', str_replace('\\', '/', dirname(__FILE__)) . '/');
define('DM_TEMPLATE_PATH', DM_PATH . 'template/');
define('DM_CONTROLLER_PATH', DM_PATH . 'controller/');
$controller = empty($_GPC['r']) ? 'store' : trim($_GPC['r']);
$action = empty($_GPC['ac']) ? 'index' : trim($_GPC['ac']);
$operation = trim($_GPC['op']);
$eid = intval($_GPC['eid']);
if(!empty($eid) && empty($_GPC['r']) && empty($_GPC['ac'])){
	checklogin();
	include $this->template("system/sysset/store_controller");
	exit;
}
$uniacid = $_W['uniacid'] = $_W['weid'] = $_GPC['i'];
$_W['uniaccount'] = $_W['account'] = uni_fetch($_W['uniacid']);
$_W['acid'] = $_W['uniaccount']['acid'];
isetcookie ('__uniacid', $uniacid, 3600*24*30);

$settings = pdo_get("deamx_food_settings",array('uniacid'=>$uniacid));
require DM_PATH . "manage.sys.inc.php";
if ($controller == 'user' && $action == 'login') {
	if (!empty($_W['deam_food']['manage']['uid'])) {//已登录状态
		itoast('', manage_url(array('r'=>'store','ac'=>'index')), 'warning');
	}
	require __forward($controller,$action);
	exit();
}
if (empty($_W['deam_food']['manage']['uid'])) {
	itoast('', manage_url(array('r'=>'user','ac'=>'login')), 'warning');
	exit();
}else{
	if(empty($_W['deam_food']['manage']['user']['status'])){
		isetcookie('__DM_food_session', false, -100);
		itoast('该账号已被禁用，无法登陆', manage_url(array('r'=>'user','ac'=>'login')), 'error');
	}
}
$store_id = $_W['deam_food']['manage']['user']['store_id'];
$storeinfo = pdo_get("deamx_food_store",array('uniacid'=>$uniacid,'id'=>$store_id), array('name'));
//$uniacid = $_W['uniacid'] = $_W['deam_food']['manage']['user']['uniacid'];
$isMobile = in_array($_W['container'], array('wechat', 'android', 'ipad', 'iphone', 'ipod'));

$frams = array(
	array(
		'title'		=>	'概况',
		'icon'		=>	'fa fa-area-chart',
		'href'		=>	manage_url(array('r'=>'store','ac'=>'index')),
		'active'	=>	($controller == 'store' && $action == 'index') ? 'active' : ''
	),
);
$user_permission = $_W['deam_food']['manage']['user']['permission'];
$user_permissionArr = explode("|", $user_permission);
if(empty($user_permissionArr)){
	isetcookie('__DM_food_session', false, -100);
	itoast('该账号无任何操作权限！', manage_url(array('r'=>'user','ac'=>'login')), 'error');
}
if(in_array('store_goods', $user_permissionArr) || in_array('store_class', $user_permissionArr)){
	$frams[] = array(
		'title'		=>	'商品',
		'icon'		=>	'fa fa-tags',
		'href'		=>	manage_url(array('r'=>'goods','ac'=>'index')),
		'active'	=>	($controller == 'goods') ? 'active' : ''
	);
}
if(in_array('store_order', $user_permissionArr)){
	$frams[] = array(
		'title'		=>	'订单',
		'icon'		=>	'fa fa-list',
		'href'		=>	manage_url(array('r'=>'order','ac'=>'index')),
		'active'	=>	($controller == 'order') ? 'active' : ''
	);
}
if(in_array('store_desk', $user_permissionArr)){
	$frams[] = array(
		'title'		=>	'桌号',
		'icon'		=>	'fa fa-cubes',
		'href'		=>	manage_url(array('r'=>'desk','ac'=>'index')),
		'active'	=>	($controller == 'desk') ? 'active' : ''
	);
}
if (in_array('store_printer', $user_permissionArr)){
	$frams[] = array(
		'title'		=>	'打印机',
		'icon'		=>	'fa fa-print',
		'href'		=>	manage_url(array('r'=>'store','ac'=>'printer')),
		'active'	=>	($controller == 'store' && $action == 'printer') ? 'active' : ''
	);
}
if (in_array('store_statistics', $user_permissionArr)){
	$frams[] = array(
		'title'		=>	'报表',
		'icon'		=>	'icon icon-baobiao',
		'href'		=>	manage_url(array('r'=>'store','ac'=>'statistics')),
		'active'	=>	($controller == 'store' && $action == 'statistics') ? 'active' : ''
	);
}
if(in_array('store_store', $user_permissionArr)){
	$frams[] = array(
		'title'		=>	'设置',
		'icon'		=>	'fa fa-cog',
		'href'		=>	manage_url(array('r'=>'store','ac'=>'settings')),
		'active'	=>	($controller == 'store' && $action == 'settings') ? 'active' : ''
	);
}
$file = __forward($controller,$action);
if (!file_exists($file)) {
	exit("文件" . $file . "不存在");
}
require $file;
exit();

function __forward($c, $a) {
	$file = DM_CONTROLLER_PATH . $c . '/' . $a . '.ctrl.php';
	return $file;
}
?>